package cn.turing.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import cn.turing.db.StudentDao;
import cn.turing.model.Student;

public class StudentEditFrame extends JFrame
{
	
	JTextField txtUsername;	//
	JTextField txthp;		//
	JTextField txtdamage;	//
	JTextField txtbeauty;	//ֵ

	JButton btnSure;	//
	JButton btnCancel;	//
	
	public StudentEditFrame(Student student)
	{
		setSize(300, 300);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		setTitle("修改");
		init(student);
		setVisible(true);
	}
	
	public void init(Student student)
	{
		
		JLabel lblUsername=new JLabel("姓名");
		JLabel lblhp=new JLabel("血量");
		JLabel lbldamage=new JLabel("伤害");
		JLabel lblbeauty=new JLabel("颜值");
		
		txtUsername=new JTextField(30);
		txthp=new JTextField(30);
		txtdamage=new JTextField(30);
		txtbeauty=new JTextField(30);
		
		btnSure=new JButton("确定");
		btnCancel=new JButton("取消");
		
		
		JPanel panel1=new JPanel();
		JPanel panel2=new JPanel();
		JPanel panel3=new JPanel();
		JPanel panel4=new JPanel();
		JPanel panel5=new JPanel();
		
		panel1.add(lblUsername);
		panel1.add(txtUsername);
		
		panel2.add(lblhp);
		panel2.add(txthp);
		
		panel3.add(lbldamage);
		panel3.add(txtdamage);
		
		panel4.add(lblbeauty);
		panel4.add(txtbeauty);
		
		panel5.add(btnSure);
		panel5.add(btnCancel);
		
		
		JPanel container=new JPanel();
		container.add(panel1);
		container.add(panel2);
		container.add(panel3);
		container.add(panel4);
		container.add(panel5);
		
		//
		txtUsername.setText(student.getUsername());
		txthp.setText(student.getHp());
		txtdamage.setText(student.getDamage());
		txtbeauty.setText(student.getBeauty());
	
		addListeners(student);
		add(container);
		
	}
	

	public void addListeners(Student student)
	{
		//确定修改
		btnSure.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				student.setUsername(txtUsername.getText());
				student.setHp(txthp.getText());
				student.setDamage(txtdamage.getText());
				student.setBeauty(txtbeauty.getText());
				
				//
				StudentDao studentDao=new StudentDao();
				int result=studentDao.edit(student);
				if(result>0)
				{
					setVisible(false);
					StudentTableModel stm=new StudentTableModel();
					MainFrame.t.setModel(stm);
				}
			}
		});
		
		//
		btnCancel.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				setVisible(false);
			}
		});
	}
	
	
}
